﻿//
// Copyright (c) 2024 BPer
//
#include <bperencoder/BPerEncoderFactory.h>
#include <bperencoder/BPerAudioEncoderAac.h>
#include <bperencoder/BPerAudioEncoderMp3.h>
#include <bperencoder/BPerAudioEncoderOpus.h>
#include <bperencoder/BPerAudioEncoderSpeex.h>

#include <bperencoder/BPerFfmpegEncoderMeta.h>
#include <bperencoder/BPerGpuEncoderFactory.h>
#include <bperencoder/BPerH265EncoderMeta.h>
#include <bperencoder/BPerH265EncoderSoft.h>
#include <bperencoder/BPerVideoEncoderFfmpeg.h>
#include <bperencoder/BPerEncoderMediacodec.h>

#if BPer_Enable_Openh264
#include <bperencoder/BPerOpenH264Encoder.h>
#else
#include <bperencoder/BPerH264EncoderMeta.h>
#include <bperencoder/BPerH264EncoderSoft.h>
#endif

BPerEncoderFactory::BPerEncoderFactory() {

}

BPerEncoderFactory::~BPerEncoderFactory() {

}
BPerVideoEncoderMeta* BPerEncoderFactory::createVideoEncoderMeta(
		BPerVideoInfo *pcontext) {
#if BPer_Enable_Ffmpeg_Codec
	if (pcontext->videoEncHwType > 0)
		return new BPerFfmpegEncoderMeta();
#endif
#if !BPer_Enable_Openh264
	if(pcontext->videoEncoderType==0) return new BPerH264EncoderMeta();
#endif
	if (pcontext->videoEncoderType == 1)
		return new BPerH265EncoderMeta();
#if BPer_Enable_Openh264
	return NULL;
#else
	return new BPerH264EncoderMeta();
#endif
}

BPerAudioEncoder* BPerEncoderFactory::createAudioEncoder(BPerAudioCodec paet,
		BPerAudioInfo *pcontext) {
	if (paet == BPer_AED_MP3)
		return new BPerAudioEncoderMp3();
	if (paet == BPer_AED_SPEEX)
		return new BPerAudioEncoderSpeex();
	if (paet == BPer_AED_OPUS)
		return new BPerAudioEncoderOpus();
	return new BPerAudioEncoderAac();
}
BPerAudioEncoder* BPerEncoderFactory::createAudioEncoder(
		BPerAudioInfo *pcontext) {
	BPerAudioCodec maet = BPer_AED_AAC;

	if (pcontext->audioEncoderType == 1)
		maet = BPer_AED_MP3;
	if (pcontext->audioEncoderType == 2)
		maet = BPer_AED_SPEEX;
	if (pcontext->audioEncoderType == 3)
		maet = BPer_AED_OPUS;
	return createAudioEncoder(maet, pcontext);
}

BPerVideoEncoder* BPerEncoderFactory::createVideoEncoder(BPerVideoCodec paet,
		BPerVideoInfo *pcontext) {
#if BPer_OS_ANDROID
	if(pcontext->videoEncHwType==0){
#if BPer_Enable_Openh264
		return new BPerOpenH264Encoder();
#else
		return  new BPerH264EncoderSoft();
#endif
	}else{
		return new BPerEncoderMediacodec();
	}
#else
	if (pcontext->videoEncHwType == 0) {
#if BPer_Enable_Openh264
		if (paet == BPer_VED_H264)
			return new BPerOpenH264Encoder();
#else
		if (paet == BPer_VED_H264)		return  new BPerH264EncoderSoft();
#endif
		if (paet == BPer_VED_H265)
			return new BPerH265EncoderSoft();
	} else {
#if BPer_Enable_GPU_Encoding
                BPerGpuEncoderFactory gf;
               return gf.createGpuEncoder();
#else
#if BPer_Enable_Ffmpeg_Codec
		return new BPerVideoEncoderFfmpeg(paet, pcontext->videoEncHwType);
#endif
#endif
	}
#if BPer_Enable_Openh264
	return new BPerOpenH264Encoder();
#else
			return  new BPerH264EncoderSoft();
#endif

#endif
}
BPerVideoEncoder* BPerEncoderFactory::createVideoEncoder(
		BPerVideoInfo *pcontext) {
	BPerVideoCodec maet = BPer_VED_H264;
	if (pcontext->videoEncoderType == 0)
		maet = BPer_VED_H264;
	if (pcontext->videoEncoderType == 1)
		maet = BPer_VED_H265;

	return createVideoEncoder(maet, pcontext);
}
